package cn.zbh.system.controller;

import cn.zbh.basic.util.AjaxResult;
import cn.zbh.basic.util.PageList;
import cn.zbh.system.domain.Permission;
import cn.zbh.system.query.PermissionQuery;
import cn.zbh.system.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(value = "菜单的查询",description="菜单相关的CRUD功能")
public class PermissionController {

    @Autowired
    private IPermissionService service;


    @GetMapping
    @ApiOperation(value = "通过ID查询所有" )
    public List<Permission> findAll(){
        return service.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Permission findById(@PathVariable("id") Long id){
        return service.findById(id);
    }

    @PostMapping
    @ApiOperation(value = "分页" )
    public PageList<Permission> query(@RequestBody PermissionQuery query){
        try {
            return service.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return  new PageList<>();
        }

    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
           service.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult updateOrAdd(@RequestBody Permission permission) {
        try {
            if (permission.getId() == null) {
                service.add(permission);
            } else {
                service.update(permission);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }



   @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            service.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }


}
